#include "doublesubsetdao.h"
#include <stdio.h>
#include <QSqlQuery>
#include <QVariant>
#include <QSqlError>
#include <QSqlDatabase>
#include "dbutil.h"
DoubleSubsetDao::DoubleSubsetDao()
{
}

DoubleSubset *DoubleSubsetDao::getDoubleSubSetById(int bsid)
{
    DoubleSubset *ds = new DoubleSubset;
    QString qSql = "select bsid,subset1,subset2,master_flg\
            from jydw_bi_subset_custom where bsid=%1";
    qSql = qSql.arg(DbUtils::getStringFormInt(bsid));
#ifdef LINUX_DB
    char sql[500];
    sprintf(sql, qSql.toLocal8Bit().data());
    DbUtils dbUtils;
    MYSQL_ROW result = dbUtils.startQuery(sql, sizeof(struct DoubleSet_result), "DoubleSubsetDao::getDoubleSubSetById");
    while (result != NULL)
    {
        DoubleSet_result *d_s = (DoubleSet_result *)result;
        ds->setBsid(d_s->bsid);
        ds->setSubset1(d_s->subset1);
        ds->setSubset2(d_s->subset2);
        ds->setMaster_flg(d_s->master_flg);
        break;
    }
    dbUtils.freeRes();
#else
    QSqlQuery query;
    query.prepare(qSql);
    if (!query.exec())
    {
        DbUtils::ErrorLog("DoubleSubsetDao::getDoubleSubSetById",
                          "SQL:" + qSql);
    }
    while (query.next())
    {
        ds->setBsid(query.value(0).toInt());
        ds->setSubset1(query.value(1).toString());
        ds->setSubset2(query.value(2).toString());
        ds->setMaster_flg(query.value(3).toInt());
        break;
    }
    query.clear();
#endif
    return ds;
}

QList<DoubleSubset> DoubleSubsetDao::getAllDoubleList()
{
    QList<DoubleSubset> s_list;
    QString str("select bsid,subset1,subset2,master_flg\
                from jydw_bi_subset_custom ");
#ifdef LINUX_DB
    char sql[500];
    sprintf(sql, str.toLocal8Bit().data());
    DbUtils dbUtils;
    MYSQL_ROW result = dbUtils.startQuery(sql, sizeof(struct DoubleSet_result), "DoubleSubsetDao::getAllDoubleList");
    while (result != NULL)
    {
        DoubleSet_result *d_s = (DoubleSet_result *)result;
        DoubleSubset ds;
        ds.setBsid(d_s->bsid);
        ds.setSubset1(d_s->subset1);
        ds.setSubset2(d_s->subset2);
        ds.setMaster_flg(d_s->master_flg);
        s_list.push_back(ds);
        result = dbUtils.getNextRow();
    }
    dbUtils.freeRes();
#else
    if (DbUtils::isDbOpen())
    {
        QSqlQuery query;
        query.prepare(str);
        if (!query.exec())
        {
            DbUtils::ErrorLog("DoubleSubsetDao::getAllDoubleList",
                              "SQL:" + str);
        }
        else
        {
            while (query.next())
            {
                DoubleSubset ds;
                ds.setBsid(query.value(0).toInt());
                ds.setSubset1(query.value(1).toString());
                ds.setSubset2(query.value(2).toString());
                ds.setMaster_flg(query.value(3).toInt());
                s_list.push_back(ds);
            }
            query.clear();
        }
    }

#endif
    return s_list;
}
